package com.example.webbrowser.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.webbrowser.Entity.WebHistory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/***
 * authro:陈润嘉
 * Date:2021/7/18
 * descprition:数据库表history的操作类
 */

public class WebHistoryDao {

    private DataBaseHelper dataBaseHelper;
    private List<WebHistory> webHistoryList;

    public WebHistoryDao(Context context) {
        //创建数据库
        dataBaseHelper = new DataBaseHelper(context,"WEB",null,1);
    }

    //增加数据
    public void add(String title,String url){
        SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
        //String sql = "insert into history(title,url) values('"+title+"','"+url+"')";
        ContentValues values = new ContentValues();
        values.put("title",title);
        values.put("url",url);
        try{
            db.insert("history",null,values);
            System.out.println(title+":"+url+"插入成功");
        }catch (Exception e){
            e.printStackTrace();
            db.close();
        }

    }

    //删除数据
    public  void delete(Integer id){
        SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
        String sql = "delete from history where id = '"+id+"'";
        try{
            db.execSQL(sql);
            System.out.println(id+"删除成功");
        }catch (Exception e){
            db.close();
        }
    }

    //删除全部数据
    public void deleteAll(){
        SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
        String sql = "delete from history";
        try{
            db.execSQL(sql);
            System.out.println("数据库全部数据清楚完毕");
        }catch (Exception e){
            db.close();
            e.printStackTrace();
        }
    }

    //查询全部数据
    public void queryAll(){
        SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
        String sql = "select * from history";
        System.out.println("查询数据库的数据：");
        try{
            Cursor cursor = db.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                String title = cursor.getString(1);
                String url = cursor.getString(2);
                System.out.println(title + url);
            }
        }catch (Exception e){
            db.close();
        }
    }


    public List<WebHistory> getWebHistoryList() {
        webHistoryList = new ArrayList<>();
        SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
        String sql = "select * from history";
        System.out.println("得到数据库全部信息");
        try{
            Cursor cursor = db.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                Integer id = cursor.getInt(0);
                String title = cursor.getString(1);
                String url = cursor.getString(2);
                System.out.println(title + url);
                WebHistory history = new WebHistory(id,title,url);
                webHistoryList.add(history);
            }
            Collections.reverse(webHistoryList);
        }catch (Exception e){
            db.close();
        }
        return webHistoryList;
    }
}
